package com.findviewbydream.evil.eda.Activity;


import android.annotation.SuppressLint;
import android.graphics.PixelFormat;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ProgressBar;

import com.findviewbydream.evil.eda.R;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

/********************************************
 * 工程名：Eda
 * 包名：com.findviewbydream.evil.eda.Activity
 * 时间：16-8-26 上午11:17
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：浏览器界面
 ********************************************/
public class WebActivity extends BaseActivity
{
    public static final String TITLE = "title";
    public static final String URL = "url";

    private WebView webView;
    private ProgressBar progressBar;
    private WebViewClient client = new WebViewClient()
    {
        /**
         * 防止加载网页时调起系统浏览器
         */
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            view.loadUrl(url);
            return true;
        }

        public void onReceivedHttpAuthRequest(WebView webview,
                                              com.tencent.smtt.export.external.interfaces.HttpAuthHandler httpAuthHandlerhost, String host,
                                              String realm)
        {
            boolean flag = httpAuthHandlerhost.useHttpAuthUsernamePassword();
        }
    };

    @Override
    public int setContentView()
    {
        return R.layout.webview;
    }

    @Override
    public String setToolbarTitle()
    {
        return getIntent().getStringExtra("title");
    }

    @Override
    public void onCreate()
    {
        initView();
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.
                SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.
                SOFT_INPUT_STATE_HIDDEN);
        webView.setWebViewClient(client);
        webView.loadUrl(getIntent().getStringExtra(URL));
        setWebView();
    }

    private void initView()
    {
        webView = (WebView) findViewById(R.id.webView);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
    }


    @SuppressLint("SetJavaScriptEnabled")
    private void setWebView()
    {
        WebSettings webSettings = webView.getSettings();

        /*设置适应屏幕*/
        {
            webSettings.setUseWideViewPort(true);//设置此属性，可任意比例缩放
            webSettings.setLoadWithOverviewMode(true);
        }
        /*支持缩放*/
        {
            webSettings.setJavaScriptEnabled(true);
            webSettings.setBuiltInZoomControls(true);
            webSettings.setSupportZoom(true);
        }
        /*支持手动输入*/
        {
            //webView.requestFocusFromTouch();
        }

        /*优先使用缓存*/
        {
            webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
            // 开启 DOM storage API 功能
            webSettings.setDomStorageEnabled(true);
        }

        webView.setWebChromeClient(new WebChromeClient()
        {
            @Override
            public void onProgressChanged(WebView view, int newProgress)
            {
                if (newProgress == 100)
                {
                    progressBar.setVisibility(View.GONE);
                }
                else
                {
                    progressBar.setProgress(newProgress);
                }
            }
        });

        /*在当前页面打开新的链接*/
        webView.setWebViewClient(new WebViewClient()
        {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                return true;
            }

            public void onReceivedHttpAuthRequest(WebView webview,
                                                  com.tencent.smtt.export.external.interfaces.HttpAuthHandler httpAuthHandlerhost, String host,
                                                  String realm)
            {
                boolean flag = httpAuthHandlerhost.useHttpAuthUsernamePassword();
            }
        });
    }

    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack())
        {
            webView.goBack();
            return true;
        }
        else
        {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}
